# 环境变量管理
# 作者: 丁文
# 时间: 2025-04-09 22:00:05
#!/bin/bash

# === Configuration Paths ===
BASE_DIR="/app/treasure-springboot2-example"
ENV_FILE="$BASE_DIR/.env"
PROFILE_D_SCRIPT="/etc/profile.d/custom_env.sh"
BACKUP_DIR="$BASE_DIR/backup"
BACKUP_ENV="$BACKUP_DIR/.env.bak"
BACKUP_SCRIPT="$BACKUP_DIR/custom_env.sh.bak"

mkdir -p "$BACKUP_DIR"

# === Apply Environment Variables ===
apply_env() {
    if [[ ! -f "$ENV_FILE" ]]; then
        echo "Error: Configuration file not found: $ENV_FILE"
        exit 1
    fi

    echo "#!/bin/bash" > "$PROFILE_D_SCRIPT"
    echo "# Automatically generated from $ENV_FILE" >> "$PROFILE_D_SCRIPT"

    while IFS= read -r line || [ -n "$line" ]; do
        [[ "$line" =~ ^\s*# ]] && continue      # Skip comments
        [[ "$line" =~ ^\s*$ ]] && continue      # Skip empty lines

        if [[ "$line" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
            echo "export $line" >> "$PROFILE_D_SCRIPT"
        else
            echo "Warning: Skipping invalid line: $line"
        fi
    done < "$ENV_FILE"

    chmod +x "$PROFILE_D_SCRIPT"
    echo "Success: Environment variables applied to $PROFILE_D_SCRIPT"
}

# === Backup Configuration and Script ===
backup_env() {
    [[ -f "$ENV_FILE" ]] && cp "$ENV_FILE" "$BACKUP_ENV" && echo "Backup: Configuration file saved to $BACKUP_ENV"
    [[ -f "$PROFILE_D_SCRIPT" ]] && cp "$PROFILE_D_SCRIPT" "$BACKUP_SCRIPT" && echo "Backup: Script saved to $BACKUP_SCRIPT"
}

# === Restore from Backup ===
restore_env() {
    [[ -f "$BACKUP_ENV" ]] && cp "$BACKUP_ENV" "$ENV_FILE" && echo "Restore: Configuration file restored from $BACKUP_ENV" || echo "Error: Configuration backup not found"
    [[ -f "$BACKUP_SCRIPT" ]] && cp "$BACKUP_SCRIPT" "$PROFILE_D_SCRIPT" && chmod +x "$PROFILE_D_SCRIPT" && echo "Restore: Script restored from $BACKUP_SCRIPT" || echo "Error: Script backup not found"
}

# === Clean Generated Script ===
clean_env() {
    if [[ -f "$PROFILE_D_SCRIPT" ]]; then
        rm -f "$PROFILE_D_SCRIPT"
        echo "Cleaned: Script deleted from $PROFILE_D_SCRIPT"
    else
        echo "Error: Script not found"
    fi
}

# === View Generated Script ===
view_env() {
    if [[ -f "$PROFILE_D_SCRIPT" ]]; then
        echo "Displaying contents of $PROFILE_D_SCRIPT:"
        cat "$PROFILE_D_SCRIPT"
    else
        echo "Error: Script not found"
    fi
}

# === Display Help Information ===
show_help() {
    echo "Usage: $0 [command]"
    echo ""
    echo "Available commands:"
    echo "  apply      Apply environment variables configuration"
    echo "  backup     Backup the .env file and generated script"
    echo "  restore    Restore the configuration and script from backup"
    echo "  clean      Delete the generated script"
    echo "  view       View the generated environment script"
    echo "  help       Display help information"
}

# === Main Logic ===
case "$1" in
    apply)
        apply_env
        ;;
    backup)
        backup_env
        ;;
    restore)
        restore_env
        ;;
    clean)
        clean_env
        ;;
    view)
        view_env
        ;;
    help|*)
        show_help
        ;;
esac

